package com.juyu.miliao.api.service;

import com.juyu.miliao.api.constant.DepositType;
import com.juyu.miliao.api.constant.EnergyLevelType;
import com.juyu.miliao.api.dao.mysql.BillDetailDao;
import com.juyu.miliao.api.dao.mysql.UserInfoDao;
import com.juyu.miliao.api.domain.BillDetail;
import com.juyu.miliao.api.domain.BillDetailDeposit;
import com.juyu.miliao.api.domain.User;
import com.juyu.miliao.api.domain.vo.BillDetailBeansVo;
import com.juyu.miliao.api.domain.vo.BillDetailDepositVo;
import com.juyu.miliao.api.util.PageUtil;
import com.juyu.miliao.common.constant.BeansType;
import com.juyu.miliao.common.constant.RecordTypeEnum;
import com.juyu.miliao.common.domain.BillDetailBeans;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class BillDetailService {
    @Resource
    private BillDetailDao billDetailDao;
    @Resource
    private UserInfoDao userInfoDao;

    private final String name ="寻豆";

    public List<BillDetail> getBillDetailByUserId(int userId, int page, int pageSize) {
        int startNum = PageUtil.getOffset(page, pageSize);
        // 获取赚取钻石的用户
        List<BillDetail> billList = billDetailDao.getBillDetailByUserId(userId, startNum, pageSize);
        if (billList.size() == 0)
            return billList;
        List<BillDetail> tempList = new ArrayList<>();
        billList.forEach(bill -> {
            // type 1:赚取钻石，2：花费金币，3：赚取金币
            //消费类型：1.购买私照 2 语音聊天 3 视频聊天 4 送礼物 5送红包
            // 6幸运红包 7金币充值 8.蜜照 9.幸运转盘消费金币 10.幸运轮盘赚取金币  11.购买qq 12.购买微信，13 偷听语句,14 聊天语音，
            // 15 抽奖,16 水果机消费金币,17 水果机赚取金币,18 收费视频 19 聊天计费 20关注公众号赚取金币,21签到赚取金币
            String message = RecordTypeEnum.getDescByType(bill.getRecordType());
            bill.setMessage(message);
            if (bill.getRecordType() == 6) {
                bill.setType(2);
                bill.setBill(bill.getSpend());
                tempList.add(bill);
                BillDetail temp = new BillDetail();
                temp.setRecordType(bill.getRecordType());
                temp.setType(3);
                temp.setBill(bill.getDiamond());
                temp.setCreateTime(bill.getCreateTime());
                tempList.add(temp);
            } else if (bill.getRecordType() == 7) {
                bill.setType(3);
                bill.setBill(bill.getSpend());
                tempList.add(bill);
            } else if (bill.getRecordType() == 9) {
                bill.setType(2);
                bill.setBill(bill.getSpend());
                tempList.add(bill);
            } else if (bill.getRecordType() == 10) {
                bill.setType(3);
                bill.setBill(bill.getDiamond());
                tempList.add(bill);
            } else if (bill.getRecordType() == 11 || bill.getRecordType() == 12) {
                bill.setType(2);
                bill.setBill(bill.getSpend());
                tempList.add(bill);
            }else if(bill.getRecordType()==13){
                if (bill.getInUserId() == userId) {
                    bill.setType(1);
                    bill.setBill(bill.getDiamond());
                } else {
                    bill.setType(2);
                    bill.setBill(bill.getSpend());
                }
                tempList.add(bill);
            }else if(bill.getRecordType()==14){
                if (bill.getInUserId() == userId) {
                    bill.setType(1);
                    bill.setBill(bill.getDiamond());
                } else {
                    bill.setType(2);
                    bill.setBill(bill.getSpend());
                }
                tempList.add(bill);
            }else if(bill.getRecordType()==15){
                bill.setType(3);
                bill.setBill(bill.getSpend());
                tempList.add(bill);
            }else if(bill.getRecordType()==16){
                bill.setType(2);
                bill.setBill(bill.getSpend());
                tempList.add(bill);
            }else if(bill.getRecordType()==17){
                bill.setType(3);
                bill.setBill(bill.getSpend());
                tempList.add(bill);
            }else if(bill.getRecordType()==20){
                bill.setType(3);
                bill.setBill(bill.getSpend());
                tempList.add(bill);
            }else if(bill.getRecordType()== RecordTypeEnum.USER_SIGN_IN_GOLD.state){
                bill.setType(3);
                bill.setBill(bill.getSpend());
                tempList.add(bill);
            }
            else {
                if (bill.getInUserId() == userId) {
                    bill.setType(1);
                    bill.setBill(bill.getDiamond());
                } else {
                    bill.setType(2);
                    bill.setBill(bill.getSpend());
                }
                tempList.add(bill);
            }

        });
        return tempList;
    }

    public List<BillDetail> getRedPacketById(int userId, int page, int pageSize) {
        int startNum = PageUtil.getOffset(page, pageSize);
        List<BillDetail> billList = billDetailDao.getRedPacketRecordById(userId, startNum, pageSize);
        if (billList.size() == 0)
            return billList;
        List<BillDetail> tempList = new ArrayList<BillDetail>();
        billList.forEach(bill -> {
            bill.setType(2);
            bill.setBill(bill.getSpend());
            tempList.add(bill);
            BillDetail temp = new BillDetail();
            temp.setType(3);
            temp.setBill(bill.getDiamond());
            temp.setCreateTime(bill.getCreateTime());
            tempList.add(temp);
        });
        return tempList;
    }

    /**
     * 查询主播的守护者
     * @param id
     * @return
     */
    public List<BillDetail> getBillDetailForUser(long id,int page, int pageSize) {
        if (page <= 0) {
            page = 1;
        }
        if (pageSize <= 0) {
            pageSize = 20;
        }
        int startNum = PageUtil.getOffset(page, pageSize);
       return billDetailDao.getBillDetailForUser(id,startNum,pageSize);
    }

    /**
     * 获取用户寻豆账单
     * @param userId
     * @param page
     * @param pageSize
     * @return
     */
    public List<BillDetailBeansVo> getBillDetailBeansByUserId(long userId, Integer page, Integer pageSize) {
        int startNum = PageUtil.getOffset(page, pageSize);
        List<BillDetailBeans> billBeansList = billDetailDao.getBillDetailBeansByUserId(userId, startNum, pageSize);
        if (billBeansList.size() == 0)
            return new ArrayList<>();
        List<BillDetailBeansVo> list = new ArrayList<>();
        for (BillDetailBeans billDetailBeans: billBeansList ) {
            BillDetailBeansVo billDetailBeansVo = new BillDetailBeansVo();
            billDetailBeansVo.setBeansType(billDetailBeans.getBeansType());
            billDetailBeansVo.setTilte(BeansType.getDescByType(billDetailBeans.getBeansType()));
            billDetailBeansVo.setCreateTime(billDetailBeans.getCreateTime());
            billDetailBeansVo.setCreateDate(billDetailBeans.getCreateDate());
            billDetailBeansVo.setUserId(userId);
            if(billDetailBeans.getBeansType()==BeansType.LUCK_DRAW_CONSUME.getCode()){
                billDetailBeansVo.setUserBeans(billDetailBeans.getBeans()+ name);
            }else if(billDetailBeans.getBeansType() == BeansType.SAN_GUO_CONSUME_BEANS.getCode()){
                billDetailBeansVo.setUserBeans(billDetailBeans.getBeans()+ name);
            }else if(billDetailBeans.getBeansType() == BeansType.SYSTEM_PUNISHMENT_GIVE_BEANS.getCode()){
                billDetailBeansVo.setUserBeans(billDetailBeans.getBeans()+ name);
            }else if(billDetailBeans.getBeansType() == BeansType.CORPSE_GAME_CONSUME__GIVE_BEANS.getCode()){
                billDetailBeansVo.setUserBeans(billDetailBeans.getBeans()+ name);
            } else {
                billDetailBeansVo.setUserBeans("+"+billDetailBeans.getBeans()+name);
            }
            list.add(billDetailBeansVo);
        }
        return list;
    }

    /**
     * 寻币账单详情
     * @param userId
     * @param page
     * @param pageSize
     * @return
     */
    public List<BillDetailDepositVo> getBillDetailDepositByUserId(long userId,Integer coinsType, Integer page, Integer pageSize) {
         User user = userInfoDao.getUserById(userId);
        int startNum = PageUtil.getOffset(page, pageSize);
        List<BillDetailDeposit> list =  billDetailDao.getBillDetailDepositByUserId(userId, startNum, pageSize);
        if(list.size() == 0)
            return new ArrayList<>();
        List<BillDetailDepositVo> billDetailDepositVoList = new ArrayList<>();
        /**
         * type 1:赚取钻石，2：花费金币，3：赚取金币 4 消耗钻石
         *  coinsType：1，寻币 2 钻石
         */
        for (BillDetailDeposit billDetailDeposit:list) {
            BillDetailDepositVo billDetailDepositVo = new BillDetailDepositVo();
            billDetailDepositVo.setCreateTime(billDetailDeposit.getCreateTime());
            billDetailDepositVo.setTilte(DepositType.getDescByType(billDetailDeposit.getRecordType()));
            billDetailDepositVo.setRecordType(billDetailDeposit.getRecordType());
            if(billDetailDeposit.getRecordType()==DepositType.PAY_VIP_ADD_BEANS.getCode()||
                    billDetailDeposit.getRecordType()==DepositType.COOL_SUMMER_GET_COIN.getCode()){
                billDetailDepositVo.setType(3);
                billDetailDepositVo.setCoinsType(1);
                billDetailDepositVo.setUserDeposit("+"+billDetailDeposit.getSpend()+"寻币");
                billDetailDepositVoList.add(billDetailDepositVo);
            }else if(billDetailDeposit.getRecordType()==DepositType.HOST_USER_PRESENTATION.getCode()){
                billDetailDepositVo.setType(4);
                billDetailDepositVo.setCoinsType(2);
                billDetailDepositVo.setUserDeposit(billDetailDeposit.getDiamond()+"钻石");
                billDetailDepositVoList.add(billDetailDepositVo);
            }else if(billDetailDeposit.getRecordType()==DepositType.HOST_USER_CANCEL_PRESENTATION.getCode()||
                    billDetailDeposit.getRecordType()==DepositType.INVITE_FRIENDS_GIVE_DIAMONDS.getCode() ||
                    billDetailDeposit.getRecordType()==DepositType.INVITE_HOST_GIVE_DIAMONDS.getCode() ||
                    billDetailDeposit.getRecordType()==DepositType.GIFT_RANKING_REWORD_DIAMONDS.getCode() ){
                billDetailDepositVo.setType(1);
                billDetailDepositVo.setUserDeposit("+"+billDetailDeposit.getDiamond()+"钻石");
                billDetailDepositVo.setCoinsType(2);
                billDetailDepositVoList.add(billDetailDepositVo);
            }else if(billDetailDeposit.getRecordType()==DepositType.SYSTEM_REWARD_GIVE_COIN.getCode()){
                billDetailDepositVo.setType(3);
                billDetailDepositVo.setCoinsType(1);
                billDetailDepositVo.setUserDeposit("+"+billDetailDeposit.getSpend()+"寻币");
                billDetailDepositVoList.add(billDetailDepositVo);
            }else if(billDetailDeposit.getRecordType()==DepositType.SYSTEM_PUNISHMENT_GIVE_COIN.getCode()){
                billDetailDepositVo.setType(2);
                billDetailDepositVo.setCoinsType(1);
                billDetailDepositVo.setUserDeposit(billDetailDeposit.getSpend()+"寻币");
                billDetailDepositVoList.add(billDetailDepositVo);
            }else if(billDetailDeposit.getRecordType()==DepositType.LUCK_DRAW_PRIZE_GET_COIN.getCode()){
                billDetailDepositVo.setType(3);
                billDetailDepositVo.setCoinsType(1);
                billDetailDepositVo.setUserDeposit("+"+billDetailDeposit.getSpend()+"寻币");
                billDetailDepositVoList.add(billDetailDepositVo);
            }else if(billDetailDeposit.getRecordType()==DepositType.NEW_USER_BINDING_PHONE_GIFT_DEPOSIT.getCode()){
                billDetailDepositVo.setType(3);
                billDetailDepositVo.setCoinsType(1);
                billDetailDepositVo.setUserDeposit("+"+billDetailDeposit.getSpend()+"寻币");
                billDetailDepositVoList.add(billDetailDepositVo);
            } else {
                if(billDetailDeposit.getOutUserId()==userId){
                    billDetailDepositVo.setType(2);
                    billDetailDepositVo.setCoinsType(1);
                    billDetailDepositVo.setUserDeposit(billDetailDeposit.getSpend()+"寻币");
                }else {
                    billDetailDepositVo.setType(1);
                    billDetailDepositVo.setCoinsType(2);
                    billDetailDepositVo.setUserDeposit("+"+billDetailDeposit.getDiamond()+"钻石");
                }
                billDetailDepositVoList.add(billDetailDepositVo);
            }
        }
        List<BillDetailDepositVo> showList = new ArrayList<>();
        for(BillDetailDepositVo billDetailDepositVo:billDetailDepositVoList){
            if(coinsType == billDetailDepositVo.getCoinsType() ){
                showList.add(billDetailDepositVo);
            }
        }
        return showList;
    }
}
